using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using RealState.BusinessObjects;

namespace RealState.DAL
{
	public class ContentsDAO
	{
		public ContentsDAO()
		{
			DbProviderHelper.GetConnection();
		}
        public List<Contents> GetContentAll()
        {
            try
            {
                List<Contents> lstContentss = new List<Contents>();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTContentAll", CommandType.StoredProcedure);
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    Contents oContents = new Contents();
                    oContents.Id = Convert.ToInt32(oDbDataReader["Id"]);
                    oContents.Rownum = Convert.ToInt32(oDbDataReader["Rownum"]);

                    if (oDbDataReader["CategoriesName"] != DBNull.Value)
                        oContents.CategoriesName = Convert.ToString(oDbDataReader["CategoriesName"]);

                    if (oDbDataReader["Title"] != DBNull.Value)
                        oContents.Title = Convert.ToString(oDbDataReader["Title"]);

                    if (oDbDataReader["Content"] != DBNull.Value)
                        oContents.Content = Convert.ToString(oDbDataReader["Content"]);

                    if (oDbDataReader["ImgPath"] != DBNull.Value)
                        oContents.ImgPath = Convert.ToString(oDbDataReader["ImgPath"]);

                    if (oDbDataReader["CategoryId"] != DBNull.Value)
                        oContents.CategoryId = Convert.ToInt32(oDbDataReader["CategoryId"]);

                    if (oDbDataReader["Duration"] != DBNull.Value)
                        oContents.Duration = Convert.ToString(oDbDataReader["Duration"]);
                    if (oDbDataReader["DistrictId"] != DBNull.Value)
                        oContents.DistrictId = Convert.ToInt32(oDbDataReader["DistrictId"]);

                    if (oDbDataReader["CityId"] != DBNull.Value)
                        oContents.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
                    oContents.CreatedTime = Convert.ToDateTime(oDbDataReader["CreatedTime"]);
                    oContents.UserID = Convert.ToInt32(oDbDataReader["UserID"]);
                    oContents.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
                    oContents.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);
                    oContents.IsSpecial = Convert.ToBoolean(oDbDataReader["IsSpecial"]);
                    oContents.Code = Convert.ToString(oDbDataReader["Code"]);
                    lstContentss.Add(oContents);
                }
                oDbDataReader.Close();
                return lstContentss;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public List<Contents> GetContentData(int Id)
        {
            try
            {
                List<Contents> lstContentss = new List<Contents>();
                Contents oContents = new Contents();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTContents", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, Id));
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    oContents.Id = Convert.ToInt32(oDbDataReader["Id"]);

                    if (oDbDataReader["Title"] != DBNull.Value)
                        oContents.Title = Convert.ToString(oDbDataReader["Title"]);

                    if (oDbDataReader["Content"] != DBNull.Value)
                        oContents.Content = Convert.ToString(oDbDataReader["Content"]);

                    if (oDbDataReader["ImgPath"] != DBNull.Value)
                        oContents.ImgPath = Convert.ToString(oDbDataReader["ImgPath"]);

                    if (oDbDataReader["CategoryId"] != DBNull.Value)
                        oContents.CategoryId = Convert.ToInt32(oDbDataReader["CategoryId"]);

                    if (oDbDataReader["Duration"] != DBNull.Value)
                        oContents.Duration = Convert.ToString(oDbDataReader["Duration"]);


                    if (oDbDataReader["DistrictId"] != DBNull.Value)
                        oContents.DistrictId = Convert.ToInt32(oDbDataReader["DistrictId"]);

                    if (oDbDataReader["CityId"] != DBNull.Value)
                        oContents.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
                    oContents.CreatedTime = Convert.ToDateTime(oDbDataReader["CreatedTime"]);
                    oContents.UserID = Convert.ToInt32(oDbDataReader["UserID"]);
                    oContents.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
                    oContents.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);
                    oContents.IsSpecial = Convert.ToBoolean(oDbDataReader["IsSpecial"]);
                    oContents.Code = Convert.ToString(oDbDataReader["Code"]);
                    lstContentss.Add(oContents);
                }
                oDbDataReader.Close();
                return lstContentss;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Contents> GetContentss()
        {
            try
            {
                List<Contents> lstContentss = new List<Contents>();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTContentss", CommandType.StoredProcedure);
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    Contents oContents = new Contents();
                    oContents.Id = Convert.ToInt32(oDbDataReader["Id"]);

                    if (oDbDataReader["Title"] != DBNull.Value)
                        oContents.Title = Convert.ToString(oDbDataReader["Title"]);

                    if (oDbDataReader["Content"] != DBNull.Value)
                        oContents.Content = Convert.ToString(oDbDataReader["Content"]);

                    if (oDbDataReader["ImgPath"] != DBNull.Value)
                        oContents.ImgPath = Convert.ToString(oDbDataReader["ImgPath"]);

                    if (oDbDataReader["CategoryId"] != DBNull.Value)
                        oContents.CategoryId = Convert.ToInt32(oDbDataReader["CategoryId"]);

                    if (oDbDataReader["Duration"] != DBNull.Value)
                        oContents.Duration = Convert.ToString(oDbDataReader["Duration"]);

                    if (oDbDataReader["DistrictId"] != DBNull.Value)
                        oContents.DistrictId = Convert.ToInt32(oDbDataReader["DistrictId"]);

                    if (oDbDataReader["CityId"] != DBNull.Value)
                        oContents.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
                    oContents.CreatedTime = Convert.ToDateTime(oDbDataReader["CreatedTime"]);
                    oContents.UserID = Convert.ToInt32(oDbDataReader["UserID"]);
                    oContents.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
                    oContents.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);

                    if (oDbDataReader["TradeTypeId"] != DBNull.Value)
                        oContents.TradeTypeId = Convert.ToInt32(oDbDataReader["TradeTypeId"]);

                    if (oDbDataReader["ContentTypeID"] != DBNull.Value)
                        oContents.ContentTypeID = Convert.ToInt32(oDbDataReader["ContentTypeID"]);

                    if (oDbDataReader["IsSpecial"] != DBNull.Value)
                        oContents.IsSpecial = Convert.ToBoolean(oDbDataReader["IsSpecial"]);
                    oContents.Code = Convert.ToString(oDbDataReader["Code"]);
                    lstContentss.Add(oContents);
                }
                oDbDataReader.Close();
                return lstContentss;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Contents GetContents(int Id)
        {
            try
            {
                Contents oContents = new Contents();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTContents", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, Id));
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    oContents.Id = Convert.ToInt32(oDbDataReader["Id"]);

                    if (oDbDataReader["Title"] != DBNull.Value)
                        oContents.Title = Convert.ToString(oDbDataReader["Title"]);

                    if (oDbDataReader["Content"] != DBNull.Value)
                        oContents.Content = Convert.ToString(oDbDataReader["Content"]);

                    if (oDbDataReader["ImgPath"] != DBNull.Value)
                        oContents.ImgPath = Convert.ToString(oDbDataReader["ImgPath"]);

                    if (oDbDataReader["CategoryId"] != DBNull.Value)
                        oContents.CategoryId = Convert.ToInt32(oDbDataReader["CategoryId"]);

                    if (oDbDataReader["Duration"] != DBNull.Value)
                        oContents.Duration = Convert.ToString(oDbDataReader["Duration"]);

                    if (oDbDataReader["DistrictId"] != DBNull.Value)
                        oContents.DistrictId = Convert.ToInt32(oDbDataReader["DistrictId"]);

                    if (oDbDataReader["CityId"] != DBNull.Value)
                        oContents.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
                    oContents.CreatedTime = Convert.ToDateTime(oDbDataReader["CreatedTime"]);
                    oContents.UserID = Convert.ToInt32(oDbDataReader["UserID"]);
                    oContents.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
                    oContents.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);

                    if (oDbDataReader["TradeTypeId"] != DBNull.Value)
                        oContents.TradeTypeId = Convert.ToInt32(oDbDataReader["TradeTypeId"]);

                    if (oDbDataReader["ContentTypeID"] != DBNull.Value)
                        oContents.ContentTypeID = Convert.ToInt32(oDbDataReader["ContentTypeID"]);

                    if (oDbDataReader["IsSpecial"] != DBNull.Value)
                        oContents.IsSpecial = Convert.ToBoolean(oDbDataReader["IsSpecial"]);
                    oContents.Code = Convert.ToString(oDbDataReader["Code"]);
                }
                oDbDataReader.Close();
                return oContents;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int AddContents(Contents oContents)
        {
            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERTContents", CommandType.StoredProcedure);
                if (oContents.Title != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title", DbType.String, oContents.Title));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title", DbType.String, DBNull.Value));
                if (oContents.Content != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Content", DbType.String, oContents.Content));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Content", DbType.String, DBNull.Value));
                if (oContents.ImgPath != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ImgPath", DbType.String, oContents.ImgPath));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ImgPath", DbType.String, DBNull.Value));
                if (oContents.CategoryId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryId", DbType.Int32, oContents.CategoryId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryId", DbType.Int32, DBNull.Value));
                if (oContents.Duration != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Duration", DbType.String, oContents.Duration));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Duration", DbType.String, DBNull.Value));
                if (oContents.DistrictId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId", DbType.Int32, oContents.DistrictId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId", DbType.Int32, DBNull.Value));
                if (oContents.CityId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId", DbType.Int32, oContents.CityId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId", DbType.Int32, DBNull.Value));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreatedTime", DbType.DateTime, oContents.CreatedTime));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserID", DbType.Int32, oContents.UserID));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsActive", DbType.Boolean, oContents.IsActive));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsRemove", DbType.Boolean, oContents.IsRemove));
                if (oContents.TradeTypeId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TradeTypeId", DbType.Int32, oContents.TradeTypeId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TradeTypeId", DbType.Int32, DBNull.Value));
                if (oContents.ContentTypeID.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ContentTypeID", DbType.Int32, oContents.ContentTypeID));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ContentTypeID", DbType.Int32, DBNull.Value));
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsSpecial", DbType.Boolean, oContents.IsSpecial));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Code", DbType.String, oContents.Code));
                return Convert.ToInt32(DbProviderHelper.ExecuteScalar(oDbCommand));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int UpdateContents(Contents oContents)
        {

            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATEContents", CommandType.StoredProcedure);
                if (oContents.Title != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title", DbType.String, oContents.Title));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title", DbType.String, DBNull.Value));
                if (oContents.Content != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Content", DbType.String, oContents.Content));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Content", DbType.String, DBNull.Value));
                if (oContents.ImgPath != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ImgPath", DbType.String, oContents.ImgPath));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ImgPath", DbType.String, DBNull.Value));
                if (oContents.CategoryId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryId", DbType.Int32, oContents.CategoryId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryId", DbType.Int32, DBNull.Value));
                if (oContents.Duration != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Duration", DbType.String, oContents.Duration));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Duration", DbType.String, DBNull.Value));
                if (oContents.DistrictId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId", DbType.Int32, oContents.DistrictId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId", DbType.Int32, DBNull.Value));
                if (oContents.CityId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId", DbType.Int32, oContents.CityId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId", DbType.Int32, DBNull.Value));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreatedTime", DbType.DateTime, oContents.CreatedTime));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserID", DbType.Int32, oContents.UserID));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsActive", DbType.Boolean, oContents.IsActive));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsRemove", DbType.Boolean, oContents.IsRemove));
                if (oContents.TradeTypeId.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TradeTypeId", DbType.Int32, oContents.TradeTypeId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TradeTypeId", DbType.Int32, DBNull.Value));
                if (oContents.ContentTypeID.HasValue)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ContentTypeID", DbType.Int32, oContents.ContentTypeID));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ContentTypeID", DbType.Int32, DBNull.Value));
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsSpecial", DbType.Boolean, oContents.IsSpecial));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Code", DbType.String, oContents.Code));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, oContents.Id));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int UpdateContentsCode(Contents oContents)
        {
            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATEContents_2", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Code", DbType.String, oContents.Code));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, oContents.Id));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RemoveContents(Contents oContents)
        {

            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEContents", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, oContents.Id));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RemoveContents(int Id)
        {

            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEContents", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, Id));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	}
}
